<?php
/* SVN FILE: $Id: account.php 255 2009-12-22 06:45:47Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Admin Plugin Ajax
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 255 $
* @lastrevision $Date: 2009-12-22 06:45:47 +0000 (Tue, 22 Dec 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-12-22 06:45:47 +0000 (Tue, 22 Dec 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/plugins/bank/publicAjax/account.php $
*/

if ( ! defined( 'IN_IPB' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
	exit();
}

class plugin_ajax_public_bank_account extends ipsAjaxCommand
{
	/**
	 * Class entry point
	 *
	 * @access	public
	 * @param	object		Registry reference
	 * @return	void		[Outputs for the ajax handler]
	 */
	public function doExecute( ipsRegistry $registry ) 
	{
		if(!marketRegistry::hasPluginPermission('use'))
		{
			$this->returnJsonError( 'nopermission' );
		}
		
		$this->request['account_id'] = intval($this->request['account_id']);
		
		$account = $this->DB->buildAndFetch(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".$this->request['account_id']));
		
		if(!$account || $account['a_mem_id'] != $this->memberData['member_id'])
		{
			$this->returnJsonError( 'nopermission' );
		}
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_market', 'bank_bank' ) );
		
		$account['a_withdraw_fee'] = numbers::multiply($account['a_withdraw_fee'],ipsRegistry::member()->getProperty('market_deduct'));
		$account['a_deposit_fee'] = numbers::multiply($account['a_deposit_fee'],ipsRegistry::member()->getProperty('market_deduct'));
		$account['a_delete_fee'] = numbers::multiply($account['a_delete_fee'],ipsRegistry::member()->getProperty('market_deduct'));
		
		$account['accountID'] = bankPluginFunctions::getBankAccountID($account);
		$account['opened'] = $this->registry->getClass('class_localization')->getDate($account['a_open'],'LONG');
		$account['currentBalance'] = marketFormatter::formatCurrency($account['balance']);
		$account['interestRate'] = marketFormatter::numberFormat($account['interest_rate'], 5, false)."%";
		$account['interestTime'] = sprintf($this->lang->words['every_x'], marketFormatter::relativeTimeFull($account['a_interest_time'], 'LONG'));
		$account['interestLast'] = $this->registry->getClass('class_localization')->getDate($account['a_last_interest'],'LONG');
		$account['withdrawFee'] = sprintf($this->lang->words['x_fee'], marketFormatter::numberFormat($account['a_withdraw_fee'], 5, false)."%");
		$account['depositFee'] = sprintf($this->lang->words['x_fee'], marketFormatter::numberFormat($account['a_deposit_fee'], 5, false)."%");
		$account['removeFee'] = sprintf($this->lang->words['x_fee'], marketFormatter::formatCurrency($account['a_delete_fee']));
		
		$account['transactions'] = array();
		$this->DB->build(array('select'=>'*','from'=>'market_bank_transactions','where'=>"l_account_id='".$account['accountID']."'",'order'=>'l_timestamp DESC','limit'=>array(10)));
		$q = $this->DB->execute();
		while($transaction = $this->DB->fetch($q))
		{
			$transaction['time'] = $this->registry->getClass('class_localization')->getDate($transaction['l_timestamp'],'LONG');
			$transaction['transaction'] = $this->lang->words['transaction_'.$transaction['l_action']];
			$transaction['amount'] = marketFormatter::formatCurrency($transaction['l_amount']);
			$transaction['fee'] = marketFormatter::formatCurrency($transaction['l_fee']);
			$transaction['balance'] = marketFormatter::formatCurrency($transaction['l_balance']);
			$account['transactions'][] = $transaction;
		}
		
		$this->returnJsonArray( $account );
	}
}